<template>
  <div class="root">
    <div class="left">
      <div class="tool">
        <div class="tool-title">矩形</div>
        <div class="tool-content">
          <div class="form">
            <div class="inputBox">
              <label for="">x</label>
              <input type="text" v-model="x" />
            </div>
            <div class="inputBox">
              <label for="">y</label>
              <input type="text" v-model="y" />
            </div>
            <div class="inputBox">
              <label for="">width</label>
              <input type="text" v-model="width" />
            </div>
            <div class="inputBox">
              <label for="">height</label>
              <input type="text" v-model="height" />
            </div>
            <div class="inputBox">
              <label for="">fillColor</label>
              <input type="color" name="" id="" v-model="fillColor" />
            </div>
            <div class="inputBox">
              <label for="">strokeColor</label>
              <input type="color" name="" id="" v-model="strokeColor" />
            </div>
            <div class="inputBox">
              <label for="">strokeWidth</label>
              <input type="text" name="" v-model="strokeWidth" />
            </div>
            <div class="inputBox">
              <button @click="drawRect">drawRect</button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
        <rect
          v-for="(item, index) in rects"
          :key="index"
          :x="item.x"
          :y="item.y"
          :width="item.width"
          :height="item.height"
          :style="`fill:${item.fillColor};stroke-width: ${item.strokeWidth};stroke: ${item.strokeColor}`"
        />
      </svg>
    </div>
  </div>
</template>
<script>
import { reactive, ref, toRefs } from "vue";
export default {
  setup() {
    let rect = {
      x: 0,
      y: 0,
      width: 0,
      height: 0,
      fillColor: "#000",
      strokeColor: "#000",
      strokeWidth: 0,
    };

    let rects = ref([
      {
        fillColor: "#ce0d0d",
        height: "444",
        strokeColor: "#500ccf",
        strokeWidth: "22",
        width: "444",
        x: "33",
        y: "33",
      },
      {
        fillColor: " rgb(95, 236, 19)",
        height: "44",
        strokeColor: "rgb(229, 215, 16)",
        strokeWidth: "4",
        width: "44",
        x: "44",
        y: "44",
      },
    ]);

    function drawRect() {
      rects.value.push(rect);
      console.log(rects.value[0]);
    }

    let reactiveRect = toRefs(reactive(rect));
    return { ...reactiveRect, rects, drawRect };
  },
};
</script>
<style lang='scss' scoped>
.root {
  .left {
    float: left;
    .tool {
      width: 200px;
      .tool-title {
        text-align: center;
      }
    }
  }
  .right {
    margin-left: 210px;
    width: 800px;
    height: 600px;
    border: 2px solid #000;
    svg {
      width: 100%;
      height: 100%;
    }
  }
}
.inputBox {
  display: flex;
  justify-content: flex-end;
  margin: 5px;
}
label {
  text-align: right;
  width: 100px;
}
input {
  display: inline-block;
  width: 60px;
}
button {
  width: 50%;
}
</style>